/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lucene;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import model.PhoneStore;
import model.Phone;
import model.Baterai;
import model.Data;
import model.Fitur;
import model.Kamera;
import model.Layar;
import model.Price;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Putra
 */
public class ReadXML{

    private PhoneStore phoneStore = new PhoneStore();
    private GetXMLComponent resourceLazada=new GetXMLComponent("D:\\Developing\\WrappingData\\LazadaPhoneStore.xml");
    public ReadXML(String path) {
        try {
            List<Phone> db = new ArrayList<Phone>();

            File fXmlFile = new File(path);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("Handphone");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    Element eBaterai = (Element) eElement.getElementsByTagName("Baterai").item(0);
                    Element eData = (Element) eElement.getElementsByTagName("Data").item(0);
                    Element eFitur = (Element) eElement.getElementsByTagName("Fitur").item(0);
                    Element eKamera = (Element) eElement.getElementsByTagName("Kamera").item(0);
                    Element eLayar = (Element) eElement.getElementsByTagName("Layar").item(0);
                    Element ePrice = (Element) eElement.getElementsByTagName("Harga").item(0);
                    
                    Phone phone = new Phone();
                    Baterai baterai = new Baterai(eBaterai.getElementsByTagName("Tipe").item(0).getNodeValue());
                    Data data = new Data(eData.getElementsByTagName("3G").item(0).getNodeValue(),eData.getElementsByTagName("Bluetooth").item(0).getNodeValue());
                    Fitur fitur = new Fitur();
                    fitur.setBrowser(eFitur.getElementsByTagName("Browser").item(0).getNodeValue());
                    fitur.setCPU(eFitur.getElementsByTagName("CPU").item(0).getNodeValue());
                    fitur.setGPS(eFitur.getElementsByTagName("GPS").item(0).getNodeValue());
                    fitur.setOS(eFitur.getElementsByTagName("OS").item(0).getNodeValue());
                    Kamera kamera = new Kamera();
                    kamera.setPrimer(eKamera.getElementsByTagName("Primer").item(0).getNodeValue());
                    kamera.setSekunder(eKamera.getElementsByTagName("Sekunder").item(0).getNodeValue());
                    Layar layar = new Layar();
                    layar.setTipe(eLayar.getElementsByTagName("Tipe").item(0).getNodeValue());
                    layar.setUkuran(eLayar.getElementsByTagName("Ukuran").item(0).getNodeValue());
                    Price price = new Price();
                    price.setTabloidPulsaPrice(ePrice.getElementsByTagName("HargaBaru").item(0).getNodeValue());
                    phone.setTitle(eElement.getElementsByTagName("title").item(0).getNodeValue());
                    
                    //get Lazada Element
                    Element eLazada=this.resourceLazada.getElementPhoneByTitle("SmartPhone", phone.getTitle());
                    price.setLazadaPrice(eLazada.getElementsByTagName("DiscountPrice").item(0).getNodeValue());
                    phone.setPrice(price);
                    
                    db.add(phone);
                }

            }

            phoneStore.db=db;

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @return the playlist
     */
    public PhoneStore getPhoneStore() {
        return phoneStore;
    }

    
}
